'''
Created on Jul 17, 2011

@author: mike
'''
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.Qt import pyqtSignal as Signal
from PyQt4.Qt import pyqtSlot as Slot
from View.Widgets.EntityEditor.ComponentEditor import ComponentEditor
from functools import partial

import random
from View.Widgets.EntityEditor import EntityDescr




class ComponentWidget(QWidget):
   
    speakNumber = Signal(int)
    speakWord = Signal(str)
    popComponentEditor = Signal( QWidget, QWidget)
    repaintEditor = Signal(QWidget)
    
    COMPONENT_HEIGHT = 50;

    
    def __init__(self, title="title", parent=None):
        
        QWidget.__init__ (self, parent) 
        self.intit = int()
        self.setMouseTracking(1)
        
        QWidget.setMaximumHeight(self, self.COMPONENT_HEIGHT);
        
        self.mComponentEditor = ComponentEditor("Editor", parent)

        self.mIsHover = 0
        
        self.setCursor(Qt.PointingHandCursor)
        
        self.mIsOpen = 0
        
        #Setup Drawable
        self.mTextPen       = QPen(Qt.white, 3, Qt.SolidLine);
        self.mFont          = QFont()
        self.mFont.setPixelSize(12)
        self.mFont.setBold(1)
        self.mText = title
        self.mImage         = None
        self.mImageHover    = None
        self.mImagePressed  = None
        self.setToolTip("Hint")
        #self.mToolTip = QToolTip(self, "hint")
        self.mHint          = "Hint"
        
        self.setMinimumHeight(50)
        self.setMaximumHeight(50)
        self.mText = title
        
    def setComponentEditor(self, component):
        self.mComponentEditor = component
        self.mImage         = component.mImage
        self.mImageHover    = component.mImageHover
        self.mImagePressed  = component.mImagePressed
        self.mTitle = component.mTitle
        
    def setIsOpen(self, isOpen):
        self.mIsOpen = isOpen
        
    def enterEvent(self, QEvent):
        '''
        '''
        self.mIsHover = 1
        
    def leaveEvent(self, QEvent):
        '''
        '''
        self.mIsHover = 0
        self.repaint()
        
    def paintEvent(self, e):
      
        qp = QPainter()
        qp.begin(self)
        
        if self.mIsOpen:
            self.drawIdle(qp)
            self.drawImageOpen(qp)
        elif self.mIsHover:
            self.drawHover(qp)
            self.drawImageHover(qp)
        else:
            self.drawIdle(qp)
            self.drawImage(qp)
            
        #Draw text
        qp.setFont(self.mFont)
        qp.setPen(self.mTextPen)
        metrics = qp.fontMetrics()
        fontBox = metrics.boundingRect(self.mText)
        
        '''textRec = QRectF(self.width() / 2 - (fontBox.width() / 2), 
                         self.height() / 2 - fontBox.height() / 2, 
                         self.width() / 2 + fontBox.width() / 2,
                         self.height() / 2 + fontBox.height() / 2)  '''
        
        textRec = QRectF(0, 
                         self.height() / 2 - 15, 
                         self.width() + fontBox.width(),
                         self.height() / 2 + fontBox.height() / 2) 
        
        qp.drawText(textRec, Qt.AlignLeft, self.mTitle);
        print(self.mText)
        
        qp.end()
        
        
        self.updateComponentEditorPoition()
        
    def drawHover(self, QPainter):
        ###########
        # GRADIENT
        ###########
        gradientRadial = QLinearGradient(0,0, 0, self.height());
        gradientRadial.setColorAt(0.0,QColor(20,20,20));
        gradientRadial.setColorAt(0.3,QColor(100,175,200));
        gradientRadial.setColorAt(0.5,QColor(75,75,75));
        # Paint
        QPainter.setPen(Qt.NoPen)
        oldBrush = QPainter.brush()
        QPainter.setBrush(gradientRadial);
        QPainter.drawRect(self.rect())
        QPainter.setBrush(oldBrush);
        
    def drawImageHover(self, QPainter):
        ##################
        # Draw image
        ##################
        centerX = self.width() / 2
        centerY = self.height() / 2
        leftX = centerX - 32 / 2
        topY = centerY - 32 / 2
        
        target = QRectF( leftX, 
                         topY, 
                         32, 32) 
        
        source = QRectF(0, 
                        0, 
                        self.mImageHover.width(), 
                        self.mImageHover.height()) 
        QPainter.drawImage(target, self.mImageHover, source)
    
    def drawImage(self, QPainter):
        ##################
        # Draw image
        ##################
        centerX = self.width() / 2
        centerY = self.height() / 2
        leftX = centerX - 32 / 2
        topY = centerY - 32 / 2
        
        target = QRectF( leftX, 
                         topY, 
                         32, 32) 
        
        source = QRectF(0, 
                        0, 
                        self.mImage.width(), 
                        self.mImage.height()) 
        QPainter.drawImage(target, self.mImage, source)
        
    def drawImageOpen(self, QPainter):
        ##################
        # Draw image
        ##################
        centerX = self.width() / 2
        centerY = self.height() / 2
        leftX = centerX - 32 / 2
        topY = centerY - 32 / 2
        
        target = QRectF( leftX, 
                         topY, 
                         32, 32) 
        
        source = QRectF(0, 
                        0, 
                        self.mImage.width(), 
                        self.mImage.height()) 
        QPainter.drawImage(target, self.mImagePressed, source)
        
    def drawIdle(self, QPainter):
        ###########
        # GRADIENT
        ###########
        gradientRadial = QLinearGradient(0,0, 0, self.height());
        gradientRadial.setColorAt(0.0,QColor(20,20,20));
        gradientRadial.setColorAt(0.3,QColor(150,150,150));
        gradientRadial.setColorAt(0.5,QColor(75,75,75));
        # Paint
        QPainter.setPen(Qt.NoPen)
        oldBrush = QPainter.brush()
        QPainter.setBrush(gradientRadial);
        QPainter.drawRect(self.rect())
        QPainter.setBrush(oldBrush);
        
        
      
      
    def mousePressEvent(self, event):
        self.intit += 1
        self.speakNumber.emit(self.intit)
        
        self.popComponentEditor.emit(self.mComponentEditor, self)
        
    def updateComponentEditorPoition(self):
        '''
        '''
       # if self.mComponentEditor:
       #     globalPos = self.mapToGlobal(self.rect().topRight())
       #     self.mComponentEditor.move(globalPos.x(),globalPos.y())
            

        
    def mouseMoveEvent(self, *args, **kwargs):
        #return QtGui.QWidget.mouseMoveEvent(self, *args, **kwargs)(self, *args, **kwargs):
        
        
        #globalPos = self.mapToGlobal(entityEditor.rect().topRight())
        #self.move(globalPos.x(),globalPos.y())
        #self.move(globalPos)
        #rect = self.rect()
        self.repaintEditor.emit(self)
        #self.update()
        return QWidget.mouseMoveEvent(self, *args, **kwargs)
        

